﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RetingManager
{
    using Core;
    using System.Reflection;

    public partial class PlugForm : Form
    {
        public PlugForm()
        {
            InitializeComponent();
            for (int i = 0; i < AppOptions.modules.Length; i++)
            {
                listView1.Items.Add("< " + AppOptions.modules[i].classname + "> " +
                AppOptions.modules[i].library);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                int i = listView1.SelectedItems[0].Index;
                listView1.SelectedItems[0].Remove();
                int count = 0;
                AppOptions.modules[i].classname = null;
                PlugInModule[] modules = new PlugInModule[AppOptions.modules.Length - 1];
                for (i = 0; i < AppOptions.modules.Length; i++)
                {
                    if (AppOptions.modules[i].classname != null)
                    {
                        modules[count] = AppOptions.modules[i];
                    }
                }
                AppOptions.modules = modules;
            }
        }
        public void Setdata(string lib, string Class)
        {
            Array.Resize(ref AppOptions.modules, AppOptions.modules.Length + 1);
            AppOptions.modules[AppOptions.modules.Length - 1] = new PlugInModule();
            AppOptions.modules[AppOptions.modules.Length - 1].classname = Class;
            AppOptions.modules[AppOptions.modules.Length - 1].library = lib;
            listView1.Items.Add("< " + AppOptions.modules[AppOptions.modules.Length - 1].classname + "> " +
                AppOptions.modules[AppOptions.modules.Length - 1].library);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            PlugAdd add = new PlugAdd();
            add.setdata = new SetData(Setdata);
            add.ShowDialog();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                int f = listView1.SelectedItems[0].Index;
                Assembly myAsm = Assembly.LoadFile(AppOptions.modules[f].library);
                _BasePlugin plug = (myAsm.CreateInstance(AppOptions.modules[f].classname) as _BasePlugin);
                if (plug != null)
                {
                    MessageBox.Show(plug.PlugInfo, "Информация о модуле.");
                }
                myAsm.Evidence.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка плагина");
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    int f = listView1.SelectedItems[0].Index;
                    Assembly myAsm = Assembly.LoadFile(AppOptions.modules[f].library);
                    _BasePlugin plug = (myAsm.CreateInstance(AppOptions.modules[f].classname) as _BasePlugin);
                    if (plug != null)
                    {
                        MessageBox.Show(plug.PlugInfo, "Информация о модуле.");
                    }
                    myAsm.Evidence.Clear();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка плагина");
            }
        }
    }
}
